﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EntitySpaces.Interfaces;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Fin_Fin005c : AdminBasePage
{
  
    decimal lendID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VLendBoxMetadata.ColumnNames.LendID] != null)
        {
            lendID = decimal.Parse(Request.QueryString[VLendBoxMetadata.ColumnNames.LendID]);
        }

    
        if (!IsPostBack)
        {
            FinLend fs = new FinLend();
            fs.LoadByPrimaryKey(lendID);

            //if (fs.LendStatus != (int)LendStatus.Pending)
            //{
            //    btnRemove.Visible = false;
            //}
        }

    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    void GridDataBind(int page)
    {

        VBoxReceiptsQuery q1 = new VBoxReceiptsQuery();
        FinLendBoxQuery q2 = new FinLendBoxQuery();

        q1.SelectAll();
        q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
        q1.Where(q2.LendID == lendID);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";

            List<decimal> boxIDs = new List<decimal>();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        Label lbReceipt = (Label)item.FindControl("lbBoxID");
                        boxIDs.Add(decimal.Parse(lbReceipt.Text));
                    }
                }

                if (boxIDs.Count > 0)
                {
                    FinLendBoxCollection lendBoxes = new FinLendBoxCollection();
                    lendBoxes.Query.Where(lendBoxes.Query.BoxID.In(boxIDs.ToArray()) && lendBoxes.Query.LendID == lendID);
                    lendBoxes.Query.Load();
                    lendBoxes.MarkAllAsDeleted();
                    lendBoxes.Save();

                    FinBoxCollection finBoxes = new FinBoxCollection();
                    finBoxes.Query.Where(finBoxes.Query.BoxID.In(boxIDs.ToArray()));
                    finBoxes.Query.Load();

                    foreach (var item in finBoxes)
                    {
                        item.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                        item.UpdatedDttm = DateTime.Now;
                        item.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    }
                    finBoxes.Save();


                }

                tr.Complete();

                GridDataBind(1);
                WebHelper.RefreshOpener();
            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}